package per.sunmes.cl.g.data;

import per.sunmes.cl.g.actor.Card;
import per.sunmes.cl.g.actor.Player;
import per.sunmes.cl.g.actor.Zone;
import per.sunmes.cl.g.i.CallAction;
import per.sunmes.cl.g.i.CallBack;
import per.sunmes.cl.g.screen.GameScreen;
import per.sunmes.cl.g.sys.U;

import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.Align;

public interface CardEffect {

	void effect(GameScreen gameScreen, Zone selfZone, Card cardSelf);

	void preEffect(GameScreen gameScreen, Zone selfZone, Card cardSelf,
			Group previewGroup);

	default void preEffectCardLand(GameScreen gameScreen, Zone zone,
			Player player, Group previewGroup) {
		if (zone == null) {
			return;
		}
		if (zone.getPlayer() != player) {
			Image pic = U.image(player.getLandPicPath());
			previewGroup.addActor(pic);
			pic.getColor().a = 0.5f;
			U.center(pic, zone);
		} else {
			Image pic = U.image(gameScreen.getOtherPlayer(player)
					.getLandPicPath());
			previewGroup.addActor(pic);
			pic.getColor().a = 0.5f;
			U.center(pic, zone);
		}
	}

	default void preEffectCardSelfLand(GameScreen gameScreen, Zone selfZone,
			Card cardSelf, Group previewGroup) {
		if (selfZone.getPlayer() != cardSelf.getPlayer()) {
			Image pic = U.image(cardSelf.getPlayer().getLandPicPath());
			previewGroup.addActor(pic);
			pic.getColor().a = 0.5f;
			U.center(pic, selfZone);
		} else {
			Image pic = U.image(gameScreen.getOtherPlayer(cardSelf.getPlayer())
					.getLandPicPath());
			previewGroup.addActor(pic);
			pic.getColor().a = 0.5f;
			U.center(pic, selfZone);
		}
	}

	default void selfCardMoveLandEffect(GameScreen gameScreen, Zone selfZone,
			Card cardSelf) {
		cardSelf.getPlayer().getHandCards().removeValue(cardSelf, true);
		U.actorKeepRectChangeParent(cardSelf, selfZone.getParent());
		cardSelf.setOrigin(Align.center);
		cardSelf.changeToSmallPic();

		float scale = (selfZone.getWidth() * 0.9f) / cardSelf.getHeight();
		cardSelf.addAction(Actions.sequence(
				Actions.parallel(Actions.scaleTo(scale, scale, 0.2f), Actions
						.moveToAligned(selfZone.getX() + selfZone.getWidth()
								/ 2,
								selfZone.getY() + selfZone.getHeight() / 2,
								Align.center, 0.2f)), new CallAction() {
					public void call() {
						selfZone.setCard(cardSelf);
						validZone(gameScreen, selfZone, cardSelf.getPlayer());
					}
				}));
	}

	default void validZone(GameScreen gameScreen, Zone zone, Player player) {
		if (zone == null) {
			return;
		}
		if (zone.getPlayer() != player) {
			zone.setPlayer(player);
		} else {
			zone.setPlayer(gameScreen.getOtherPlayer(player));
		}
	}

	default void selfCardMoveLandEffect(GameScreen gameScreen, Zone selfZone,
			Card cardSelf, CallBack landedCall) {
		cardSelf.getPlayer().getHandCards().removeValue(cardSelf, true);
		U.actorKeepRectChangeParent(cardSelf, selfZone.getParent());
		cardSelf.setOrigin(Align.center);
		cardSelf.changeToSmallPic();

		float scale = (selfZone.getWidth() * 0.9f) / cardSelf.getHeight();
		cardSelf.addAction(Actions.sequence(
				Actions.parallel(Actions.scaleTo(scale, scale, 0.2f), Actions
						.moveToAligned(selfZone.getX() + selfZone.getWidth()
								/ 2,
								selfZone.getY() + selfZone.getHeight() / 2,
								Align.center, 0.2f)), new CallAction() {
					public void call() {
						landedCall.call();
					}
				}));
	}

	boolean isCouldPutToZone(Zone zone);
}
